<?php
/**
 * @filesource actors_controller.php
 * K秀之信
 */
class Controller_Actors extends Controller_Abstract{

    /**
     * 执行控制器动作之前调用
     */
    protected function _before_execute() {
    	preg_match('/([0-9]{3})\..*/', $_SERVER['SERVER_ADDR'], $matches);
    	if( in_array($matches[1], array('127','192') ) ){
    		$this->_view['actorReg'] = 'http://'.$_SERVER['SERVER_ADDR'].'/union/index.php?controller=partner&action=actor';
    	}else{
    		$this->_view['actorReg'] = 'http://service.kkyoo.com/union/index.php?controller=partner&action=actor';
    	}
    }
    
	// K秀之星首页
	function actionIndex(){
		//1. K秀力推
		$this->_view['daren'] = $this->votesTop('result', " and e.daren = '1' ", '0,8');
		
		//2. K秀红人
		$this->_view['hongren'] = $this->votesTop('result', '', '0,12', ' e.dygift desc, e.votes desc ');
		
		//3. 3个排行榜
		$this->sidebar(true, true, true);
	}
	
	// 个人详细信息
	function actionInfo(){
		$uin = intval($this->_context->uin);
		$sql = 'SELECT *, a.uin as user_id from ( select * FROM kkyoo_rooms.tbl_rooms_actor WHERE `uin` = '.$uin
		  .' ) as a left join kkyoo_rooms.tbl_rooms_actor_ext e on a.uin = e.uin; ';
		$info = $this->getOne($sql);
		
		if( !empty($info) ){
			$table =QDB::getConn();
			$table->execute('SET NAMES latin1');
			$sqlNick = 'select nick from kexoo_im.basic_tbl where uin = '.$uin;
			$reNick = $table->execute($sqlNick);
			$row = $reNick->fetchRow();
			
			$info['nick'] = iconv('GB18030', 'UTF-8//IGNORE', $row['nick']);
			$table->execute('SET NAMES utf8');
			$reNick->free();
			
			$this->_view['info'] = $info;
			
			// 2个排行榜
			$this->sidebar(false, true, true);
		}else{
			return $this->_redirect( url('actors/index') );
		}
		//dump($info);
	}
	
	
	// K秀力推 more 
	function actionDaren(){
		$this->_view['page_title'] = 'K秀力推';
		$page = intval($this->_context->page);
		if ($page < 1) $page = 1;
		$total = $this->votesTop('count', " and e.daren = '1' ");
		
		if( (int)$total['total'] > 0 ){
			$pager = $this->getPager( (int)$total['total'], $page, 20);
			$this->_view['person'] = $this->votesTop('result', " and e.daren = '1' ", $pager['limit']);
			$this->_view['pagination'] = $pager['pagination'];
		}
		//dump($this->_view);
		// 2个排行榜
		$this->sidebar(false, true, true);
	}
	
	// K秀红人 more
	function actionHongren(){
		$this->_view['page_title'] = 'K秀红人';
		$page = intval($this->_context->page);
		if ($page < 1) $page = 1;
		$total = $this->votesTop('count', '');
		
		if( (int)$total['total'] > 0 ){
			$pager = $this->getPager( (int)$total['total'], $page, 20);
			$this->_view['person'] = $this->votesTop('result', '', $pager['limit'], ' e.dygift desc, e.votes desc ');
			$this->_view['pagination'] = $pager['pagination'];
		}
		
		// 2个排行榜
		$this->sidebar(false, true, true);
		$this->_viewname = 'daren';
	}
	
	// 新人新秀 more 
	function actionNewbie(){
		$this->_view['page_title'] = '新人新秀 ';
		$page = intval($this->_context->page);
		if ($page < 1) $page = 1;
		$time = time() - 2678400;
		$total = $this->votesTop('count', ' and a.updated >= '.$time );
		
		if( (int)$total['total'] > 0 ){
			$pager = $this->getPager( (int)$total['total'], $page, 20);
			$this->_view['person'] = $this->votesTop('result', ' and a.updated >= '.$time, $pager['limit']);
			$this->_view['pagination'] = $pager['pagination'];
		}
		
		// 2个排行榜
		$this->sidebar(false, true, true);
		$this->_viewname = 'daren';
	}
	
	// 活跃榜 more
	function actionLive(){
		$this->_view['page_tilte'] = '活跃榜';
		$page = intval($this->_context->page);
		if ($page < 1) $page = 1;
		$total = $this->votesTop('count', '', '0,10', 'e.room_time');
		
		if( (int)$total['total'] > 0 ){
			$pager = $this->getPager( (int)$total['total'], $page, 20);
			$this->_view['person'] = $this->votesTop('result', '', $pager['limit'], ' e.room_time desc ');
			$this->_view['pagination'] = $pager['pagination'];
		}
		
		// 2个排行榜
		$this->sidebar(false, true, true);
		$this->_viewname = 'daren';
	}
	
    // 男女歌手人气榜, 新人新秀, 活跃榜
	function sidebar($songer=false, $newbie=false, $live=false){
		if( $songer ){
			//女歌手:
			$this->_view['female'] = $this->votesTop('result', " and a.sex = 'female' ", '0,10');
			
			//男歌手:
			$this->_view['male'] = $this->votesTop('result', " and a.sex = 'male' ", '0,10');
		}
		
		if( $newbie ){ //新人新秀 - 申请成功1个月内  2678400s.
			$time = time() - 2678400;
			$this->_view['newbie'] = $this->votesTop('result', ' and a.updated >= '.$time , '0,10');
		}
		
		/*if( $live ){ //活跃榜 - 按房间上麦排
			$this->_view['live'] = $this->votesTop('result', '', '0,10', 'e.room_time');
		}*/
	}
	
	//显示投票排行
	function votesTop($type='count', $where='', $limit='0,10', $order='e.votes desc'){
		if($type =='count'){
			$sql = " SELECT count(e.uin) as total from kkyoo_rooms.tbl_rooms_actor a, kkyoo_rooms.tbl_rooms_actor_ext e "
				." where a.status = '1' and a.uin = e.uin ".$where;
			return $this->getOne($sql);
		}else{
			$sql = 'SELECT ae.*, bt.nick from ( '
			  	." SELECT a.uin, e.votes, a.pic_url from kkyoo_rooms.tbl_rooms_actor a, kkyoo_rooms.tbl_rooms_actor_ext e "
			  	." where a.status = '1' and a.uin = e.uin ".$where.' order by '.$order.' limit '.$limit
			  	.' ) ae, kexoo_im.basic_tbl bt where bt.uin = ae.uin ';
			
			$table =QDB::getConn();
			$table->execute('SET NAMES latin1');
			$us = $this->getAll($sql);
			$table->execute('SET NAMES utf8');
			foreach ($us as $k=>$v){
				$us[$k]['nick'] = iconv('GB18030', 'UTF-8//IGNORE', $v['nick']);
			}
		  	return $us;
		}
	}
	
}//END class
?>